<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="120px">
					<el-row>
						<el-col :span="6">
							<el-form-item label="最近修改时间">
								<el-date-picker v-model="tableData.dateRange" type="datetimerange" size="default"
									style="width: 300px" range-separator="-" start-placeholder="开始日期"
									end-placeholder="结束日期"></el-date-picker>
							</el-form-item>
						</el-col>

						<el-col :span="6">
							<el-form-item label="库存" prop="inventory">
								<el-select v-model="tableData.param.inventory" placeholder="请选择库存" clearable>
									<el-option v-for="item in dictionary.inventoryList" :label="item.label"
										:value="item.value" :key="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item label="状态" prop="status">
								<el-select v-model="tableData.param.status" placeholder="请选择状态" clearable >
									<el-option v-for="item in dictionary.statusList3" :label="item.label"
										:value="item.value" :key="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-table v-loading="loading" :data="tableData.data" row-key="uniqueKey" :tree-props="{ children: 'child' }">
				<el-table-column label="省级" prop="name" min-width="100px">
					<template #default="{ row }">{{ !row.pid ? row.name : '' }}</template>
				</el-table-column>
				<el-table-column label="市级" prop="name" min-width="100px">
					<template #default="{ row }">{{ row.pid ? row.name : '' }}</template>
				</el-table-column>
				<el-table-column label="库存" align="center" min-width="100px">
					<template #default="{ row }">{{ dictionary[row.inventory] }}</template>
				</el-table-column>
				<el-table-column label="月租价" align="center" prop="monthlyRent" min-width="100px" />
				<el-table-column label="话费/分钟" align="center" prop="price" min-width="100px" />
				<el-table-column label="状态" align="center" min-width="100px">
					<template #default="{ row }">
						<el-switch v-if="row.status" v-model="row.status" active-value="ENABLED" :disabled="!permissionStore.isPermissionsBtn('010001002')"
							inactive-value="DISABLED" @change="handleStatusChange(row)"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="最近修改时间" align="center" prop="updateTimeTxt" min-width="100px"/>
				<el-table-column label="操作" align="center" v-if="permissionStore.isPermissionsBtn('010001001')"
					class-name="small-padding" min-width="200px" fixed="right">
					<template #default="scope">
						<el-button  v-if="permissionStore.isPermissionsBtn('010001001') && scope.row.pid" text type="primary"
							@click="handleUpdate(scope.row)"><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<editView ref="editRef" @getList="getList"></editView>
	</div>
</template>
<script lang="ts">
import { editCityDataApi, treeCityDataApi } from '@/api/numberManage/inventoryConfiguration';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission"
import dictionary from '@/utils/dictionary.json'
import editView from './component/edit.vue';
import { updateParamsWithDateRange, formatDateTime } from '@/utils/index'
export default defineComponent({
	name: 'enterpriseList', //企业列表
	components: {
		editView,
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const editRef = ref();
		const permissionStore = usePermissionStore()
		// 是否显示所有搜索选项
		const showAll = ref(false);

		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				dateRange: [],

				param: {
					status:undefined,
					inventory:undefined,
					beginTime:undefined,
					endTime:undefined,
				},
			},
		});

		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮登录 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			state.tableData.dateRange = []
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			const obj = updateParamsWithDateRange(state.tableData.dateRange)
			Object.assign(state.tableData.param, obj);
			treeCityDataApi(state.tableData.param).then((res: any) => {
				let list = res.data ?? [];
				list.forEach(item => {
					item.uniqueKey = `${item.id}-${item.pid}`;
					if (item.child) {
						item.child.forEach(child => {
							child.uniqueKey = `${child.id}-${child.pid}`;
							child.updateTimeTxt=formatDateTime(child.updateTime)
						});
					}
				});

				state.tableData.data = list
				console.log(state.tableData.data);
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		const toggleSearch = () => {
			showAll.value = !showAll.value;
		};
		const handleUpdate = (row) => {
			editRef.value.openDialog(toRaw(row));
		};

		// 状态修改
		const handleStatusChange = (row: any) => {
			let text =  row.status == 'ENABLED' ? '启用' : '禁用';
			ElMessageBox.confirm('确认要[' + text  + row.name + ']吗?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				const { id, monthlyRent, price, inventory, status } = row
					let obj = {
						id,
						monthlyRent,
						price,
						inventory,
						status,	//状态
					}
				return editCityDataApi(obj);
			}).then(() => {
				ElMessage.success(text + "成功");
			}).catch(function () {
				row.status = row.status == 'ENABLED' ? 'DISABLED' : 'ENABLED';
			});
		};
		return {
			proxy,
			editRef,
			showAll,
			loading,
			word,
			queryRef,
			permissionStore,
			resetQuery,
			getList,
			toggleSearch,
			handleUpdate,
			dictionary,
			handleStatusChange,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
